<?php
/**
 * Created by PhpStorm.
 * User: yiban
 * Date: 2017/9/20
 * Time: 上午11:13
 */

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\Learn;
class LearnController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('学习信息');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('学习信息');
            $content->description('编辑');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('学习信息');
            $content->description('新增');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Learn::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->uname('用户名');
            $grid->grade('班级');
            $grid->intime('进入时间');
            $grid->outtime('出去时间');
            $grid->up('升班');
            $grid->down('末班');
            $grid->upclass('上一班级');
            $grid->teacher('任课老师');
            $grid->adviser('班主任');
            $grid->college('学院');
            $grid->desc('备注');
            $grid->created_at('创建时间');

            $grid->tools(function (Grid\Tools $tools) {
                $tools->batch(function (Grid\Tools\BatchActions $actions) {
                    $actions->disableDelete();
                });
            });

            $grid->disableExport();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Learn::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('uname', '用户名');
            $form->text('grade', '班级');
            $form->datetime('inttime', '进入时间');
            $form->datetime('outtime', '出去时间');
            $form->text('up', '升班');
            $form->text('down', '末班');

            $form->text('upclass', '上一班级');
            $form->text('teacher', '任课老师');
            $form->text('adviser', '班主任');
            $form->text('college', '学院');
            $form->textarea('desc', '备注');

            $form->display('created_at', '创建时间');
            $form->display('updated_at', '更新时间');
        });
    }

    public function update($id)
    {
        return $this->form()->update($id);;
    }

    public function store()
    {
        return $this->form()->store();
    }

    public function destroy($id)
    {
        if ($this->form()->destroy($id)) {
            return response()->json(['msg' => 'delete success!']);
        }
    }
}